package com.ylsf.blog.service.impl;

import com.ylsf.blog.mapper.UserMapper;
import com.ylsf.blog.pojo.User;
import com.ylsf.blog.service.LoginService;
import com.ylsf.blog.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

/**
 * 登录服务实现类
 */
public class LoginServiceImpl implements LoginService {

    @Override
    public User selectByUsername(String username) {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectUserByUsername(username);
        SqlSessionUtil.close(sqlSession);
        return user;
    }


    @Override
    public boolean judgeLogin(String username, String password) {
        boolean flag=false;
        SqlSession sqlSession = SqlSessionUtil.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users = userMapper.selectAllUser();
        for(User user:users){
            if (username.equals(user.getUsername())&&password.equals(user.getPassword())) {
                flag=true;
            }
        }
        SqlSessionUtil.close(sqlSession);
        return flag;
    }
}
